package OtherPubs;

import Rout.*;
import java.util.ArrayList;
import javax.script.ScriptException;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.annotations.*;

public class PopCatcher implements TestCase{

    private final GoBefore browser = new GoBefore();
    private final ArrayList<String> urls = new ArrayList<String>();
    private final ArrayList<String> uags = new ArrayList<String>();
    private final JavascriptExecutor js = (JavascriptExecutor) browser.getDriver();
    private final int attempt = 3;   // how much we'll try for catch events
    private WebElement downlButton;
    private ArrayList<String> newTab;
    private static final String TESTRUN = "\n Run test: ", TESTCASE = "\n\nTest case: ", TESTNAME = "test_", NOTFOUND = "Ads not found";
    private CsvFileWriter toCsv;
    
    @BeforeMethod
    public void Before() throws Exception {
                
        toCsv = new CsvFileWriter(9);
        
        urls.add("http://pmn.perfecttrkloop.com/?s1={S1}&s2={S2}&s3={S3}&s4={S4}&s5={S5}");
        urls.add("http://engine.trklnks.com/link.engine?guid=fdb724e4-cdea-4b68-ba31-676a1c4ce5ab&Hardlink=true&time=0");
        urls.add("http://engine.trklnks.com/link.engine?guid=c00f2813-bc69-4ae3-8cbd-2d492fd02bd3&Hardlink=true&time=0");
        urls.add("http://engine.trklnks.com/link.engine?guid=9f4d83e3-381c-4c1c-9b10-bb43849270da&Hardlink=true&time=0");       
        
        System.out.println(TESTRUN + getClass().getSimpleName());
        browser.setXpFilename(null);
//        browser.setRepDir(getClass().getSimpleName());
        browser.setEnableSafeBrow(Boolean.FALSE);
        browser.setChDrvPath("/home/user/Dropbox/WebDriver/lin64/chromedriver");
//        browser.setRemoteDriver("192.168.6.77"); //Win7Clone VBox
//        browser.setRemoteDriver("192.168.4.170"); //VMWare net sbapshot selen-ready
//        browser.setProxy("49.151.104.242", "3128"); //Ph
    }
    public String getReferal(WebDriver driver, String url) throws ScriptException {
        String refURL = (String) js.executeScript("return document.referrer");
//        System.out.println("REF: " + refURL);
        return refURL;
    }

    public void closeAlerts() throws Exception {
        try {
            browser.getDriver().switchTo().alert().accept();
        } catch (Exception e) {
        }
    }

    @Test //Void Footer Baner
    public void Test() throws Exception {                           

        for (String url : urls) {

            browser.setCurURL(url);
            System.out.println(TESTCASE + (urls.indexOf(url) + 1));
            
            browser.setUp();
            
            try {
                browser.getDriver().switchTo().alert();
                browser.getClicker().sendKeys(Keys.ENTER).build().perform();
            } catch (Exception e) {
                closeAlerts();
            }

//            browser.ipDetect();
            browser.setRepDir(TESTNAME + (urls.indexOf(url) + 1) + "_" + browser.detectGEO());

            //closeAlerts();
            browser.refreshCurlURL(1, 1);

            System.out.println("\nTest URL is: " + url);
            for (int m = 0; m < attempt; m++) {
                
                System.out.println("attempt :" + m);
               

//                if (url.contains("videomega")) {
//		     Thread.sleep(2000);
//                     try {
//                         
//                        WebElement but = browser.getDriver().findElement(By.className(""));
//                        browser.getClicker().click(but).build().perform();
//                    } catch (Exception e) {
//                    }
//                    
//                } else {
                
                //Click On Milk
                Thread.sleep(6000);
                browser.getClicker().moveByOffset(0, 135).click().build().perform();
                
                Thread.sleep(6000);
//		}
		
                closeAlerts();
//Get number of open tabs            
                newTab = new ArrayList<String>(browser.getDriver().getWindowHandles());
                if (newTab.size() > 1) {
                    System.out.print("Open new tab.");
                    Thread.sleep(5000);
                    System.out.println(" Total open tabs is: " + newTab.size());
                    for (int i = 1; i < newTab.size(); i++) {
//If 2 tabs present, switch to new tab  

                        try {
                            browser.getDriver().switchTo().window(newTab.get(i));
                            System.out.println("Switch to tab # " + i);
                            closeAlerts();
                            getReferal(browser.getDriver(), browser.getDriver().getCurrentUrl());

                            closeAlerts();
                            Thread.sleep(1000);

                            String NextUrl = browser.getDriver().getCurrentUrl();
                            Thread.sleep(3000);
                            browser.takeFoto();

                            System.out.println("Redirect to: " + NextUrl);
                            System.out.println("Redirect via URL: " + getReferal(browser.getDriver(), NextUrl));
                            System.out.println("!" + browser.getFotoName() + "|thumbnail!");

                        } catch (Exception e) {                            
                        }

                    }

                } else {
                    System.out.println(NOTFOUND);
                }
                toCsv.addData("");                
            }
            closeAlerts();
            browser.getDriver().quit();
            Thread.sleep(1000);
        }
    
//    }
//        browser.getDriver().close();
    }

    @AfterMethod
    public void TearsDown() throws Exception {
        browser.getDriver().quit();

    }

}
